﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "WallPositionPoints.h"
#include "DbObjectUtils.h"
#include "IRegeneratorDataIdCreator.h"
#include "IElementBasicInformation.h"
#include "IDocument.h"
#include "IRegenerator.h"
#include "IGenericElement.h"
#include "Wall.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;
using namespace Sample;

Vector3d WallMiddlePoint(const Wall* pWall)
{
#if 0
    Vector3d dir = pWall->GetData()->GetEndPoint() - pWall->GetData()->GetStartPoint();
    double dLen = dir.Length();
    dir.Normalize();
    return pWall->GetData()->GetStartPoint() + dir * dLen * 0.5;
#endif
    return (pWall->GetData()->GetStartPoint() + pWall->GetData()->GetEndPoint()) * 0.5;
}

DBOBJECT_DATA_DEFINE(WallPositionPoints)
{
    SetOwnerElement(nullptr);
}

Wall* WallPositionPoints::GetWall()
{
    IGenericElement *pGenericElement = quick_cast<IGenericElement>(GetOwnerElement());
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pGenericElement, L"GenericElement不能为空", L"GDMP", L"2023-09-30");
    Wall* pWall = quick_cast<Wall>(pGenericElement->GetExternalObject());
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pWall, L"pModelLine不能为空", L"GDMP", L"2023-09-30");
    return pWall;
}

const Wall* WallPositionPoints::GetWall() const
{
    const IGenericElement *pGenericElement = quick_cast<IGenericElement>(GetOwnerElement());
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pGenericElement, L"GenericElement不能为空", L"GDMP", L"2023-09-30");
    const Wall* pWall = quick_cast<Wall>(pGenericElement->GetExternalObject());
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pWall, L"pModelLine不能为空", L"GDMP", L"2023-09-30");
    return pWall;
}

bool WallPositionPoints::SetOwnerElement(IElement* pOwnerElement)
{
    m_pOwnerElement = pOwnerElement;
    return true;
}

IElement* WallPositionPoints::GetOwnerElement()
{
    return m_pOwnerElement;
}


const IElement* WallPositionPoints::GetOwnerElement() const
{
    return m_pOwnerElement;
}

int WallPositionPoints::GetControlPointCount() const
{
    return 4;
}

gcmp::Vector3d WallPositionPoints::GetControlPoint(int index) const
{
    DBG_WARN_AND_RETURN_UNLESS(index < GetControlPointCount(), Vector3d::Zero,
        L"索引不能超过控制点个数", L"GDMP", L"2023-05-30");
    const Wall* pWall = GetWall();
    Vector3d point;
    if (index == 0)
    {
        point = pWall->GetData()->GetStartPoint();
    }
    else if (index == 1)
    {
        point = pWall->GetData()->GetEndPoint();
    }
    else if (index == 2)
    {
        point = WallMiddlePoint(pWall);
    }
    else
    {
        point = WallMiddlePoint(pWall);
        point.SetZ(point.GetZ() + pWall->GetData()->GetHeight());
    }
    return point;
}

void WallPositionPoints::SetControlPoint(int index, const Vector3d& point)
{
    Wall* pWall = GetWall();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pWall, L"pWall不能为空", L"GDMP", L"2023-11-08");
    WallData* pData = pWall->GetData();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pData, L"pData不能为空", L"GDMP", L"2023-11-08");
    if (index == 0)
    {
        Vector3d ptEnd = pData->GetEndPoint();
        ptEnd.SetZ(point.GetZ());
        pData->SetStart(point);
        pData->SetEnd(ptEnd);
    }
    else if (index == 1)
    {
        Vector3d ptStart = pData->GetStartPoint();
        ptStart.SetZ(point.GetZ());
        pData->SetStart(ptStart);
        pData->SetEnd(point);
    }
    else if (index == 2)
    {
        Vector3d translate = point - WallMiddlePoint(pWall);
        pData->SetStart(pData->GetStartPoint() + translate);
        pData->SetEnd(pData->GetEndPoint() + translate);
    }
    else if (index == 3)
    {
        Vector3d translate = point - WallMiddlePoint(pWall);
        if (translate.Dot(Vector3d::UnitZ) <= 0)
        {
            return;
        }
        pData->SetHeight(translate.Z());
    }
    pWall->MarkDataRdId();

    IGenericElement* pGenericElement = quick_cast<IGenericElement>(GetOwnerElement());
    DBG_WARN_AND_RETURN_VOID_UNLESS(pGenericElement, L"pGenericElement不能为空", L"GDMP", L"2023-11-08");
    if (const IElementPositionPoints* pElementPositionPoints = pGenericElement->GetPositionPoints())
    {
        for (int i = 0; i < pElementPositionPoints->GetControlPointCount(); ++i)
        {
            MarkControlPointRdId(i);
        }
    }

}

RegenDataId WallPositionPoints::GetControlPointRdId(int index) const
{
    static int dataId = IRegeneratorDataIdCreator::CreateRegenDataId(
        WSTRING_OF("WallPositionPoints"), 
        WSTRING_OF("Sample" + "WallPositionPoints"));
    return RegenDataId(GetOwnerElement()->GetBasicInformation()->GetElementId().AsInt64(), dataId, index);
}

void WallPositionPoints::MarkControlPointRdId(int index)
{
    GetOwnerElement()->GetDocument()->GetRegenerator()->MarkRegenDataId(GetControlPointRdId(index));
}

gcmp::NdbObject* WallPositionPoints::GetTopOwnerObject() const
{
    return quick_cast<gcmp::NdbObject>(m_pOwnerElement);
}

